﻿#region FileHeader
//////////////////////////////////////////////////////////////////////////////////
// Author:                          Hector Llanos
// Date:                            08.27.2010
// FileName:                        CCharacterSelectState.cs
// Reason:                          pick which character you will like to be
//////////////////////////////////////////////////////////////////////////////////
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

using Run_Game;
using Globals;
using Objects;
using Manager;

namespace GameState
{
    public class CCharacterSelectState : IGameState
    {
        private ContentManager m_pContent;
        private SpriteBatch m_pSpriteBatch;

        private int m_nSelection;

        private const int MAXITEMS = 2;//(int)CGlobal.Players.MAXPLAYERS;
        private KeyboardState oldState;
        private SpriteFont m_sfFontSprite;

        private string[] m_szCharacterNames;

        public void Enter(ContentManager content, SpriteBatch spriteBatch)
        {
            m_pContent = content;
            m_pSpriteBatch = spriteBatch;

            m_sfFontSprite = content.Load<SpriteFont>("Fonts/ARCADECLASSIC");

            m_szCharacterNames = new string[2];//[(int)CGlobal.Players.MAXPLAYERS];

            m_szCharacterNames[0] = "ArmyJoe";
            m_szCharacterNames[1] = "Ninja";

            Game1.Instance.CharacterFactory.RegisterClassType<CArmyJoe>("ArmyJoe");
            Game1.Instance.CharacterFactory.RegisterClassType<CNinja>("Ninja");

            oldState = Keyboard.GetState();
        }
        public void Input()
        {
            KeyboardState newState = Keyboard.GetState();

            //if the left or right key is pressed
            // Is the UP key down?
            if (newState.IsKeyDown(Keys.A))
            {
                if (!oldState.IsKeyDown(Keys.A))
                {
                    m_nSelection--;

                    if (m_nSelection < 0)
                        m_nSelection = MAXITEMS - 1;
                }
            }

            // Is the  right
            if (newState.IsKeyDown(Keys.D))
            {
                if (!oldState.IsKeyDown(Keys.D))
                {
                    m_nSelection++;

                    if (m_nSelection >= MAXITEMS)
                        m_nSelection = 0;
                }
            }
            // Is the ENTER key down?
            if (newState.IsKeyDown(Keys.Enter))
            {
                if (!oldState.IsKeyDown(Keys.Enter))
                {
                    //get which character player 1 select
                    Game1.Instance.CharacterSelect[(int)CGlobal.Players.PLAYER1] = (CCharacterBase.CharacterType)m_nSelection + 1;

                    // change to the game
                    Game1.Instance.ChangeState(new CGamePlayState());
                    Game1.Instance.PushState(new CHudState());
                }
            }

            // Update saved state.
            oldState = newState;

        }
        public void Update(TimeSpan fElapsedTime)
        {

        }
        public void Render()
        {
            string textToRender = "Character Selection";

            // Find the center of the string
            Vector2 FontOrigin = m_sfFontSprite.MeasureString(textToRender) / 2;

            //draw Character Selection
            m_pSpriteBatch.DrawString(m_sfFontSprite, textToRender, new Vector2(Game1.Instance.Graphics.PreferredBackBufferWidth / 2, 200), Color.Red, 0, FontOrigin, 1.5f, SpriteEffects.None, 0.5f);

            //draw which character is being selected
            textToRender = m_szCharacterNames[m_nSelection];
            FontOrigin = m_sfFontSprite.MeasureString(textToRender) / 2;
            m_pSpriteBatch.DrawString(m_sfFontSprite, textToRender, new Vector2(Game1.Instance.Graphics.PreferredBackBufferWidth / 2, 500), Color.Red, 0, FontOrigin, 1.5f, SpriteEffects.None, 0.5f);
        
        }
        public void Exit()
        {

        }
    }
}
